package com.yanqu.road.dao.impl.nenmonopolymarket;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.systemgroup.ResourceServerGroup;
import com.yanqu.road.entity.systemgroup.newmonopolymarket.NewMonopolyMarketWeekStateData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NewMonopolyMarketWeekStateDataDaoImpl extends TempDataObjectDao<NewMonopolyMarketWeekStateData> {

    public static String getNewMonopolyMarketWeekStateDataSql = "select * from `t_c_new_monopoly_market_week_state_data` where `weekId`=?";
    public List<NewMonopolyMarketWeekStateData> getNewMonopolyMarketWeekStateData(int weekId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<NewMonopolyMarketWeekStateData> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getNewMonopolyMarketWeekStateDataSql);
            int i = 1;
            call.setInt(i++, weekId);
            rs = call.executeQuery();
            while (rs.next()) {
                NewMonopolyMarketWeekStateData data = parse(rs);
                list.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String updateNewMonopolyMarketWeekStateDataSql = "update `t_c_new_monopoly_market_week_state_data` set openDays=?,serverIdList=?,innerGroupList=?,maxGroupId=?,activeNum=?,npcBlood=?,officeRefreshTime=?,kingResGrabRefreshTime=?,queenResGrabRefreshTime=?,battleEndHandle=?,rankReward=?,resGrabReward=?,lastStatTime=? where weekId=? and systemGroupId=?";
    @Override
    protected boolean updateData(NewMonopolyMarketWeekStateData newMonopolyMarketWeekStateData) {
        newMonopolyMarketWeekStateData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketWeekStateData.getOpenDays()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(newMonopolyMarketWeekStateData.getServerIdList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(newMonopolyMarketWeekStateData.getInnerGroupList())));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketWeekStateData.getMaxGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketWeekStateData.getActiveNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(newMonopolyMarketWeekStateData.getNpcBlood())));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketWeekStateData.getOfficeRefreshTime()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketWeekStateData.getKingResGrabRefreshTime()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketWeekStateData.getQueenResGrabRefreshTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, newMonopolyMarketWeekStateData.isBattleEndHandle()));
        para.put(i++, new DbParameter(Types.BOOLEAN, newMonopolyMarketWeekStateData.isRankReward()));
        para.put(i++, new DbParameter(Types.BOOLEAN, newMonopolyMarketWeekStateData.isResGrabReward()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketWeekStateData.getLastStatTime()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketWeekStateData.getWeekId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketWeekStateData.getSystemGroupId()));
        if (getDb().executeNoneQuery(updateNewMonopolyMarketWeekStateDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertNewMonopolyMarketWeekStateDataSql = "insert into `t_c_new_monopoly_market_week_state_data`(weekId,systemGroupId,openDays,serverIdList,innerGroupList,maxGroupId,activeNum,npcBlood,officeRefreshTime,kingResGrabRefreshTime,queenResGrabRefreshTime,battleEndHandle,rankReward,resGrabReward,lastStatTime) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(NewMonopolyMarketWeekStateData newMonopolyMarketWeekStateData) {
        newMonopolyMarketWeekStateData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketWeekStateData.getWeekId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketWeekStateData.getSystemGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketWeekStateData.getOpenDays()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(newMonopolyMarketWeekStateData.getServerIdList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(newMonopolyMarketWeekStateData.getInnerGroupList())));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketWeekStateData.getMaxGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketWeekStateData.getActiveNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(newMonopolyMarketWeekStateData.getNpcBlood())));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketWeekStateData.getOfficeRefreshTime()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketWeekStateData.getKingResGrabRefreshTime()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketWeekStateData.getQueenResGrabRefreshTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, newMonopolyMarketWeekStateData.isBattleEndHandle()));
        para.put(i++, new DbParameter(Types.BOOLEAN, newMonopolyMarketWeekStateData.isRankReward()));
        para.put(i++, new DbParameter(Types.BOOLEAN, newMonopolyMarketWeekStateData.isResGrabReward()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMarketWeekStateData.getLastStatTime()));
        if (getDb().executeNoneQuery(insertNewMonopolyMarketWeekStateDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected NewMonopolyMarketWeekStateData parseData(ResultSet rs) throws SQLException {
        NewMonopolyMarketWeekStateData data = new NewMonopolyMarketWeekStateData();
        data.setWeekId(rs.getInt("weekId"));
        data.setSystemGroupId(rs.getInt("systemGroupId"));
        data.setOpenDays(rs.getInt("openDays"));
        data.setServerIdList(JSON.parseObject(rs.getString("serverIdList"), new TypeReference<List<Long>>() {}));
        data.setInnerGroupList(JSON.parseObject(rs.getString("innerGroupList"), new TypeReference<Map<Integer, ResourceServerGroup>>() {}));
        data.setMaxGroupId(rs.getInt("maxGroupId"));
        data.setActiveNum(rs.getInt("activeNum"));
        data.setOfficeRefreshTime(rs.getInt("officeRefreshTime"));
        data.setKingResGrabRefreshTime(rs.getInt("kingResGrabRefreshTime"));
        data.setQueenResGrabRefreshTime(rs.getInt("queenResGrabRefreshTime"));
        data.setBattleEndHandle(rs.getBoolean("battleEndHandle"));
        data.setRankReward(rs.getBoolean("rankReward"));
        data.setResGrabReward(rs.getBoolean("resGrabReward"));
        data.setLastStatTime(rs.getInt("lastStatTime"));
        String str = rs.getString("npcBlood");
        if(StringUtils.isNullOrEmpty(str)){
            data.setNpcBlood(JSON.parseObject(rs.getString("npcBlood"), new TypeReference<Map<Integer, Map<Integer, Long>>>() {}));
        }

        return data;
    }
}
